#include "Scene\Mesh.h"

Mesh::Mesh()
	: m_is_empty(TRUE),
	  m_center_position(0.0f,0.0f,0.0f),
	  m_indexes(NULL),
	  m_index_buffer(NULL),
	  m_vertex_buffer(NULL),
	  m_material(NULL)
{
}

Mesh::~Mesh()
{
	m_indexes.clear();
	delete m_index_buffer;
}

BOOL Mesh::build(aiMesh* ai_mesh, UINT interval,Material* material,VertexBuffer* vertex_buffer)
{
	if(!ai_mesh->mNumFaces || !ai_mesh->mNumVertices)
	{
		return FALSE;
	}

	if(m_is_empty)
	{
			m_material = material;
			m_vertex_buffer = vertex_buffer;
	}

	for(UINT i = 0 ; i < ai_mesh->mNumFaces ; i++)
	{
		m_indexes.push_back(ai_mesh->mFaces[i].mIndices[0] + interval);
		m_indexes.push_back(ai_mesh->mFaces[i].mIndices[1] + interval);
		m_indexes.push_back(ai_mesh->mFaces[i].mIndices[2] + interval);
	}

	m_index_buffer = new IndexBuffer(m_indexes.data(),m_indexes.size());

	if(!m_index_buffer)
	{
		return FALSE;
	}

	m_is_empty = false;


	return TRUE;
}

CONST std::vector<UINT>& Mesh::indexes() CONST
{
	return m_indexes;
}

CONST D3DXVECTOR3 Mesh::center() CONST
{
	return m_center_position;
}

VOID Mesh::setCenter(D3DXVECTOR3 center)
{
	m_center_position = center;
}

IndexBuffer* Mesh::GetIndexBuffer() const
{
	return m_index_buffer;
}

VertexBuffer* Mesh::GetVertexBuffer() const
{
	return m_vertex_buffer;
}

Material* Mesh::GetMaterial() const
{
	return m_material;
}